package com.ss.demo.springbootdemo.reflect;

import com.ss.demo.springbootdemo.entity.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 作者: MrZhangJR
 * 时间: 2021/11/8 13:43
 * 描述: 通过类对象创建对象
 */
public class ReflectCreateObje {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<Person> personClass = Person.class;
        // 通过newInstance() 利用无参构造创建
        Person person = personClass.newInstance();
        // 通过constructor创建
        Constructor<Person> constructor = personClass.getConstructor();
        Person person1 = constructor.newInstance();
        // 通过constructor创建带参
        Constructor<Person> constructor1 = personClass.getConstructor(String.class);
        constructor1.newInstance("zjr");

    }
}
